/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.core;

import java.util.ArrayList;

import com.bluestone.util.Util;

/**
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 * 
 */
public class CheckTrueAction extends CheckAction {

	/**
	 * 
	 * @param type String that defines the type of the action.
	 * @param target String that defines the graphical elements operated by action.
	 * @param value String that defines the values need to be check.
	 * @param delay String that defines the delay time after the action finished.
	 * @param offset String that defines the offset of the graphical elements.
	 */
	public CheckTrueAction(String type, String target, String value,
			String delay, String offset) {
		super(type, target, value, delay, offset);
	}

	/**
	 * Check the pageContent whether contain the values.
	 * @return true if the pageContents does contain all of the values.
	 */
	public boolean execute() {		
		long start = System.currentTimeMillis();
		boolean isTrue = doCheckTrue();
		long end = System.currentTimeMillis();
		long counts = end - start;
		if (this.getTimeout() != null) {
			long time = Long.valueOf(this.timeout).longValue();
			while (!isTrue && counts <= time) {
				isTrue = doCheckTrue();
				end = System.currentTimeMillis();
				counts = end - start;
			}
		}
		if (delay != null) {
			robot.delay(Util.getDelayTime(Integer.parseInt(delay)));
		} else {
			robot.delay(Util.getDelayTime(Util.CHECKDELAY));
		}
		return isTrue;
	}

	private boolean doCheckTrue() {
		if (!super.execute()){
			return false;
		}
		ArrayList list = split(value);
		for(int i=0; i<list.size(); i++){
			String temp = (String)list.get(i);
			if (pageContent.indexOf(temp) < 0) {
				Util.warn("cant find '"+temp +"' in "+pageContent);
				return false;
			}			
		}
/*		
		String[] values = value.split(this.spitRegex);
		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				String temp = values[i];
				if(temp.startsWith("@")){
					temp = Util.getString(temp);
				}
				if (pageContent.indexOf(temp) < 0) {
					Util.warn("cant find '"+temp +"' in "+pageContent);
					return false;
				}
			}
		}
*/		
		if (target == null || target.length() == 0){
			return true;
		}

		return true;
	}
	
	ArrayList split(String str){
		ArrayList list = new ArrayList();
		String [] values = str.split(this.spitRegex);
		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				String temp = values[i].trim();
				if(temp.startsWith("@")){
					temp = Util.getString(temp);
					list.addAll(split(temp));
				}else{
					list.add(temp);
				}
			}
		}
		return list;
	}

}
